﻿using Microsoft.EntityFrameworkCore;
using NewsPublish.Model.Entity;
using NewsPublish.Model.Request;
using NewsPublish.Model.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace NewsPublish.Service
{
    /// <summary>
    /// 新闻评论 服务接口
    /// </summary>
    public class CommentService
    {
        private Db _db;
        private NewsService _newsService;

        public CommentService(Db db, NewsService newsService)
        {
            this._db = db;
            this._newsService = newsService;
        }


        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="comment">评论请求类</param>
        /// <returns>成功返回评论详情信息,失败则不返回详情</returns>
        public ResponseModel AddComment(AddComment comment)
        {
            var news = _newsService.GetOneNews(comment.NewsId);//根据新闻ID获取新闻信息
            if (news.code == 0)
            {
                return new ResponseModel { code = 0, result = "新闻不存在" };
            }
            else
            {
                var com = new NewsComment { AddTime = DateTime.Now, NewsId = comment.NewsId, Contents = comment.Contents };
                _db.NewsComment.Add(com);

                int i = _db.SaveChanges();
                if (i > 0)
                {
                    return new ResponseModel
                    {
                        code = 200,
                        result = "新闻评论添加成功",
                        data = new
                        {
                            contents = comment.Contents,
                            floor = "#" + (news.data.ConmmentCount + 1),
                            addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                        }
                    };
                }
                else
                {
                    return new ResponseModel { code = 0, result = "新闻评论添加失败" };
                }
            }
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="commentID">评论ID</param>
        /// <returns></returns>
        public ResponseModel DeleteComment(int commentID)
        {
            var comment = _db.NewsComment.Find(commentID);

            if (comment == null)
            {
                return new ResponseModel { code = 0, result = "评论不存在" };
            }
            else
            {
                _db.NewsComment.Remove(comment);
                int i = _db.SaveChanges();
                if (i > 0)
                {
                    return new ResponseModel { code = 200, result = "新闻评论删除成功" };
                }
                else
                {
                    return new ResponseModel { code = 200, result = "新闻评论删除失败" };
                }
            }
        }

        /// <summary>
        /// 获取评论集合
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns>评论集合</returns>
        public ResponseModel GetCommentsList(Expression<Func<NewsComment, bool>> where)
        {
            var comments = _db.NewsComment.Include("News").Where(where).OrderBy(c => c.AddTime).ToList();
            var response = new ResponseModel
            {
                code = 200,
                result = "评论获取成功"
            };

            response.data = new List<CommentModel>();

            int floor = 1;
            foreach (var comment in comments)
            {
                response.data.Add(new CommentModel
                {
                    Id = comment.Id,
                    NewsName = comment.News.Title,
                    Contents = comment.Contents,
                    AddTime = comment.AddTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    Remark = comment.Remark,
                    Floor = "#" + floor
                });
                floor++;
            }

            response.data.Reverse();
            return response;
        }


    }
}
